<template>
  <div>
    <!-- 1.0 面包屑组件 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- el-table 表格分区 -->
    <!-- data 显示的数据（数组） -->
    <!-- el-table-column 表格列 -->
    <!-- prop 对应列表内容的字段名（字符串） -->
    <!-- label 列表 -->
    <!-- 2.0 卡片组件 card -->
    <el-card class="box-card">
      <!-- 3.0 表格组件 Table -->
      <el-table :data="postList" style="width: 100%">
        <el-table-column prop="id" label="" width="60"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 1">
              <i class="el-icon-tickets"></i>
              <span>文章</span>
            </span>
            <span v-else>
              <i class="el-icon-video-camera"></i>
              <span>视频</span>
            </span>
          </template>
        </el-table-column>

        <el-table-column label="封面" width="200">
          <template slot-scope="scope">
            <img
              class="cover"
              v-if="scope.row.cover.length"
              :src="scope.row.cover[0].url | imgUrl"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者昵称" width="200">
        </el-table-column>
        <el-table-column label="操作" width="200">
          <!-- 作用域插槽 - 自定义列模板  -->
          <template slot-scope="scope">
            {{ scope.row.id }}
            <el-button
              size="mini"
              @click="$router.push(`/home/post_publish/${scope.row.id}`)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 
      sizes 一共有多少分页
      prev  === 上一页
      next === 下一页
      jumper === 搜索到第几页
      total===一共有多少条数据
     -->
    <!-- 4.0 分页组件 paggination 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getPostAPI } from "@/apis/user";
export default {
  data() {
    return {
      postList: [],
      pageIndex: 1,
      pageSize: 4,
      total: 0,
    };
  },
  async created() {
    const res = await getPostAPI({ pageSize: 1000 });
    console.log(res);
    // 获取总条数(后端的锅)
    this.total = res.data.data.length;
    this.PostgetIndex();
  },
  methods: {
    async PostgetIndex() {
      const res2 = await getPostAPI({
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      });
      console.log(res2);
      this.postList = res2.data.data;
    },

    // 获取文章列表数据2 - 用于分页展示的
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.PostgetIndex();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.PostgetIndex();
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}
.cover {
  width: 50px;
  height: 50px;
}
.block {
  margin-top: 20px;
  text-align: center;
}
</style>